import java.util.ArrayList;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;

import org.hyperic.hibernate.Util;
import org.hyperic.hq.hqu.rendit.BaseController
import org.hyperic.hibernate.SortField
import org.hyperic.hq.hqu.rendit.html.DojoUtil
import org.hyperic.hq.hqu.rendit.html.HtmlUtil

import SopMssField
import MssMessage

class NeuroController 
	extends BaseController
{
//	private static SopMssField fId = new SopMssField("ID");
	private static SopMssField fSbbId = new SopMssField("SBB ID");
	private static SopMssField fAppId = new SopMssField("App ID");
	private static SopMssField fMsgTypeId = new SopMssField("Msg type ID");
	private static SopMssField fLevel = new SopMssField("Level");
	private static SopMssField fCategory = new SopMssField("Category");
	private static SopMssField fText = new SopMssField("Description");
	private static SopMssField fTimestamp = new SopMssField("Date/Time");

	private Connection m_conn;
	private PreparedStatement m_prepStat;
	
	private List<MssMessage> m_items = new ArrayList<MssMessage>();
	
	private updateMssData(pageInfo) {
		m_items.clear();
		
		m_prepStat.setInt(1, pageInfo.getPageSize());
		m_prepStat.setInt(2, pageInfo.getStartRow());
		ResultSet res = m_prepStat.executeQuery();
		
		if (res != null) {
			while (res.next()) {
				MssMessage msg = new MssMessage();
			
				msg.setId(res.getLong(1));
				msg.setSbbId(res.getString(2));
				msg.setAppId(res.getString(3));
				msg.setMsgTypeId(res.getString(4));
				msg.setLevel(res.getString(5));
				msg.setCategory(res.getString(6));
				msg.setText(res.getString(7));
				msg.setTimestamp(res.getDate(8));
			
				m_items.add msg
			}
		}
	}
	
	private SOP_SHEMA =  [
	getData: {pageInfo, params -> updateMssData(pageInfo); m_items },
	defaultSort: fTimestamp,
	defaultSortOrder: 0,
	columns: [
//	[field:fId, label:{it.getId()}, width:'0%'],
	[field:fLevel, label:{it.getLevel()}],
	
	[field:fCategory, label:{it.getCategory()}],
	
	[field:fTimestamp, label:{it.getTimestamp()}],
	
	[field:fText, label:{it.getText()}],

	[field:fSbbId, label:{it.getSbbId()}],
	
	[field:fAppId, label:{it.getAppId()}],
	
	[field:fMsgTypeId, label:{it.getMsgTypeId()}]
	]
	]
	
	protected void init() {
		onlyAllowSuperUsers()
		setJSONMethods(['getTableData'])
		
		m_conn = Util.getConnection()
		m_prepStat = m_conn.prepareStatement("SELECT `ID`,`SBB_ID`,`APP_ID`,`MSG_TYPE_ID`,`LEVL`,`CATEGORY`,`TEXT`,`TIMESTAMP` FROM MESSAGES LIMIT ? OFFSET ?")
	}
	
	def index(params) {
		// By default, this sends views/freezer/index.gsp to
		// the browser, providing 'plugin' and 'userName' locals to it
		//
		// The name of the currently-executed action dictates which .gsp file 
		// to render (in this case, index.gsp).
		//
		// If you want to render AJAX, read RenderFrame.groovy for parameters
		// or return a Map from this method and in init(), call:
		//     setJSONMethods(['myJSONMethod', 'anotherJSONMethod'])
		render(locals:[ plugin :  getPlugin(),
		userName: user.name])  
	}
	
	def demo(params) {
		render(locals:[ sopMsgShema : SOP_SHEMA ])
	}
	
	def getTableData(params) {
		DojoUtil.processTableRequest(SOP_SHEMA, params)
	}
		
	def treeData(params) {  
		def json =  "{ identifier: 'name',\n" +
		"label: 'name',\n" +
		"items: [\n" +
		"{ name:'Africa', type:'continent', population:'900 million', area: '30,221,532 sq km',\n" +
		"timezone: '-1 UTC to +4 UTC',\n" +
		"children:[{_reference:'Egypt'}, {_reference:'Kenya'}, {_reference:'Sudan'}] },\n" +
		"{ name:'Egypt', type:'country' },\n" +
		"{ name:'Kenya', type:'country',\n" +
		"children:[{_reference:'Nairobi'}, {_reference:'Mombasa'}] },\n" +
		"{ name:'Nairobi', type:'city' },\n" +
		"{ name:'Mombasa', type:'city' },\n" +
		"{ name:'Sudan', type:'country',\n" +
		"children:{_reference:'Khartoum'} },\n" +
		"{ name:'Khartoum', type:'city' },\n" +
		"{ name:'Asia', type:'continent',\n" +
		"children:[{_reference:'China'}, {_reference:'India'}, {_reference:'Russia'}, {_reference:'Mongolia'}] },\n" +
		"{ name:'China', type:'country' },\n" +
		"{ name:'India', type:'country' },\n" +
		"{ name:'Russia', type:'country' },\n" +
		"{ name:'Mongolia', type:'country' },\n" +
		"{ name:'Australia', type:'continent', population:'21 million',\n" +
		"children:{_reference:'Commonwealth of Australia'}},\n" +
		"{ name:'Commonwealth of Australia', type:'country', population:'21 million'},\n" +
		"{ name:'Europe', type:'continent',\n" +
		"children:[{_reference:'Germany'}, {_reference:'France'}, {_reference:'Spain'}, {_reference:'Italy'}] },\n" +
		"{ name:'Germany', type:'country' },\n" +
		"{ name:'France', type:'country' },\n" +
		"{ name:'Spain', type:'country' },\n" +
		"{ name:'Italy', type:'country' },\n" +
		"{ name:'North America', type:'continent',\n" +
		"children:[{_reference:'Mexico'}, {_reference:'Canada'}, {_reference:'United States of America'}] },\n" +
		"{ name:'Mexico', type:'country',  population:'108 million', area:'1,972,550 sq km',\n" +
		"children:[{_reference:'Mexico City'}, {_reference:'Guadalajara'}] },\n" +
		"{ name:'Mexico City', type:'city', population:'19 million', timezone:'-6 UTC'},\n" +
		"{ name:'Guadalajara', type:'city', population:'4 million', timezone:'-6 UTC' },\n" +
		"{ name:'Canada', type:'country',  population:'33 million', area:'9,984,670 sq km',\n" +
		"children:[{_reference:'Ottawa'}, {_reference:'Toronto'}] },\n" +
		"{ name:'Ottawa', type:'city', population:'0.9 million', timezone:'-5 UTC'},\n" +
		"{ name:'Toronto', type:'city', population:'2.5 million', timezone:'-5 UTC' },\n" +
		"{ name:'United States of America', type:'country' },\n" +
		"{ name:'South America', type:'continent',\n" +
		"children:[{_reference:'Brazil'}, {_reference:'Argentina'}] },\n" +
		"{ name:'Brazil', type:'country', population:'186 million' },\n" +
		"{ name:'Argentina', type:'country', population:'40 million' }\n" +
		"]}"
		render(inline:"/* ${json} */", contentType: 'text/json-comment-filtered')  
	}
}
